package com.ruoyi.utils;

import com.ruoyi.common.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 解析字符串工具类
 *
 * @author Mr-Le
 */
public class ParseString {

    /**
     * 解析Tag中的字符串的个数
     *
     * @param str 待解析字符串
     * @return
     */
    public static Map<String, Integer> getSameStrNum(String str) {
        Map<String, Integer> map = new HashMap<>();
        if (!str.isEmpty()) {
            String replace = str.replace("\"", "").replace("[", "").replace("]", "");
            String[] split = replace.split(",");
            for (int i = 0; i < split.length; i++) {
                Integer integer = map.get(split[i]);
                map.put(split[i], integer == null ? 1 : integer + 1);
            }
            return map;
        }
        return map;
    }

    /**
     * String类型 计算其中子串出现个数，并封装为json字符串
     *
     * @param str 目标串
     * @return json格式字符串
     */
    public static String getTagNum(String str) {

        StringBuilder builder = new StringBuilder();
        Map<String, Integer> sameStrNum = getSameStrNum(str);
        Set<Map.Entry<String, Integer>> entries = sameStrNum.entrySet();
        builder.append("{");
        for (Map.Entry<String, Integer> entry : entries) {
            builder.append("\"");
            builder.append(entry.getKey());
            builder.append("\":");
            builder.append(entry.getValue());
            builder.append(",");
        }
        builder.append("}");
        builder.deleteCharAt(builder.length() - 2);
        return builder.toString();
    }

    /**
     * 解析字符串，根据文件名得到时间信息
     *
     * @param str 文件名，格式如：170471_21-04-02-19-27-13_1.jpg
     * @return 字符串，格式如：2021-04-02 19:27:13
     */
    public static String string2Date(String str) {
        String resStr = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        if (!str.isEmpty()) {
            String replaceFirst = str.replaceFirst("(.?.?.?.?.?.?)_", "20");
            String[] split = replaceFirst.split("\\.");
            resStr = split[0];
            try {
                String substring = StringUtils.substring(resStr, 0, -2);
                parse = simpleDateFormat.parse(substring);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            return format.format(parse);
        }
        return resStr;
    }

    /**
     * 根据 imageUrl 获取文件名
     * 1、先截掉 ？ 之后的内容
     * 2、在分割 / 之后的内容
     * 3、取最后的内容在处理
     * "imgUrl":"http://cdn.cqimg.zjtpyun.com/zhibao/CQ2_pic/ID_170555/170555_21-04-09-05-33-49_1.jpg?x-oss-process=style/cq7-orignal",
     */
    public static String handleUrlToName(String url) {
        StringBuilder builder = new StringBuilder(url);
        int i = builder.indexOf("?");
        builder.delete(i, builder.length());
        int lastIndexOf = builder.lastIndexOf("/");
        builder.delete(0, lastIndexOf + 1);
        builder.insert(0, "Time: ");
        builder.replace(21, 22, " ");
        builder.replace(24, 25, ":");
        builder.replace(27, 28, ":");
        String s = builder.toString();
        String replaceFirst = s.replaceFirst("(.?.?.?.?.?.?)_", "20");
        String[] split = replaceFirst.split("\\.");
        String resStr = split[0];
        return StringUtils.substring(resStr, 0, -2);
    }
}
